package com.sky.controller.Workers;

import com.sky.dto.*;
import com.sky.entity.WorkOrder;
import com.sky.entity.Worker;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.service.WorkerService;
import com.sky.service.impl.WorkerServiceImp;
import com.sky.vo.EmployeeLoginVO;
import com.sky.vo.WorkerLoginVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("workers")
public class WorkerControl {
    @Autowired
    private WorkerService workerService;

    @Autowired
    private OrderService orderService;

    @PostMapping("/login")
    public Result<WorkerLoginVO> login(@RequestBody WorkerLoginDTO workerLoginDTO) {
        log.info("工人登录：{}", workerLoginDTO);
        Worker worker= workerService.login(workerLoginDTO);


        WorkerLoginVO workerLoginVO=WorkerLoginVO.builder()
                .id(worker.getId())
                .name(worker.getName())
                .workerid(worker.getWorkid())
                .build();
        return Result.success(workerLoginVO);
    }
    //退出登录
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }
    //工人修改个人信息
    @PutMapping
    @ApiOperation("修改个人信息")
    public Result update(@RequestBody WorkerDTO workerDTO){
        log.info("编辑员工信息",workerDTO);
        workerService.update(workerDTO);
        return Result.success();
    }
    @GetMapping("/status/{campus_building}")
    @ApiOperation("根据校区楼号查询信息")
    public Result selectOrders (@PathVariable ReportOrder reportOrder){
        log.info("工人按校区和楼号查询订单",reportOrder);
        ArrayList<WorkOrder> arrayList=new ArrayList<>();
        arrayList=orderService.selectOrder(reportOrder);
        return Result.success(arrayList);
    }

    //查询订单
    @GetMapping("/status")
    @ApiOperation("查询各状态订单")
    public Result selectStatusOrders(@RequestBody String status){
        log.info("查询各状态订单",status);
        ArrayList<WorkOrder> arrayList=new ArrayList<>();
        arrayList=orderService.selectStatusOrders(status);
        return Result.success(arrayList);
    }

    //接单
    @PutMapping("/confirm")
    @ApiOperation("工人接单")
    public Result confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        log.info("工人接单");
        orderService.confirm(ordersConfirmDTO);
        return Result.success();
    }
    //工人批量接单
    @PostMapping("/batchUpdateOrders")
    @ApiOperation("工人批量接单")
    public Result batchUpdateOrders(@RequestBody List<Long> orderIds){
        log.info("工人批量接单");
        orderService.batchUpdateOrders(orderIds);
        return Result.success();
    }
    //工人完成订单
    @PutMapping("/finish")
    @ApiOperation("工人完成订单")
    public Result finish(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        log.info("工人完成订单");
        orderService.finish(ordersConfirmDTO);
        return Result.success();
    }
    //工人批量完成订单
    @PutMapping("/batchUpdateOrdersfinish")
    @ApiOperation("工人批量完成订单")
    public Result batchUpdateOrdersfinish(@RequestBody List<Long> orderIds) {
        log.info("工人批量完成接单");
        orderService.batchUpdateOrdersfinish(orderIds);
        return Result.success();
    }

}
